<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      style="margin: 15px 0px"
    >
      <el-form-item label="模型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="模型简介" prop="introduction">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.introduction"-->
      <!--          placeholder="请输入模型简介"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="执行时间" prop="executionTime">
        <el-date-picker
          clearable
          v-model="queryParams.executionTime"
          type="month"
          placeholder="请选择执行时间"
          value-format="yyyy-MM"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
        <!--        <el-date-picker clearable-->
        <!--          v-model="queryParams.executionTime"-->
        <!--          type="date"-->
        <!--          value-format="yyyy-MM-dd"-->
        <!--          placeholder="请选择执行时间">-->
        <!--        </el-date-picker>-->
      </el-form-item>
      <el-form-item label="执行结果" prop="status">
        <el-select
          v-model="queryParams.result"
          placeholder="请选择执行结果"
          clearable
        >
          <el-option
            v-for="dict in dict.type.execution_history_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="执行结果" prop="result">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.result"-->
      <!--          placeholder="请输入执行结果"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="模型ID" prop="modelId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.modelId"-->
      <!--          placeholder="请输入模型ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['executionHistory:executionHistory:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['executionHistory:executionHistory:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['executionHistory:executionHistory:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['executionHistory:executionHistory:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table
      v-loading="loading"
      :data="executionHistoryList"
      @selection-change="handleSelectionChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      <el-table-column label="ID编码" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" :index="indexMethod" />
      <el-table-column label="模型名称" align="center" prop="name" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="模型简介" align="center" prop="introduction" />
      <el-table-column
        label="执行时间"
        align="center"
        prop="executionTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.executionTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行结果" align="center" prop="result">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.execution_history_result"
            :value="scope.row.result"
          />
        </template>
      </el-table-column>

      <!--      <el-table-column label="结果审计报告" align="center" prop="report" />-->
      <!--      <el-table-column label="执行结果详情" align="center" prop="detail" />-->
      <!--      <el-table-column label="模型ID" align="center" prop="modelId" />-->
      <!--      <el-table-column label="疑点数据数量" align="center" prop="doubtCount" />-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding 80px"
      >
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="warning"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['executionHistory:executionHistory:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['executionHistory:executionHistory:edit']"
            >结果</el-button
          >
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="warning"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['executionHistory:executionHistory:edit']"-->
          <!--          >报告</el-button>-->
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['executionHistory:executionHistory:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改历史记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentCode">
          <!--          <el-input v-model="form.department" placeholder="请输入部门" />-->
          <treeselect
            v-model="form.departmentCode"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="模型简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入模型简介" />
        </el-form-item>
        <el-form-item label="执行时间" prop="executionTime">
          <el-date-picker
            clearable
            v-model="form.executionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择执行时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="执行结果" prop="result">
          <!--          <el-input v-model="form.result" placeholder="请输入执行结果" />-->
          <el-select v-model="form.result" placeholder="请输入执行结果">
            <el-option
              v-for="dict in dict.type.execution_history_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结果审计报告" prop="report">
          <el-input
            v-model="form.report"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="执行结果详情" prop="detail">
          <el-input
            v-model="form.detail"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="疑点数据数量" prop="doubtCount">
          <el-input
            v-model="form.doubtCount"
            placeholder="请输入疑点数据数量"
          />
        </el-form-item>
        <!--        <el-form-item label="模型ID" prop="modelId">-->
        <!--          <el-input v-model="form.modelId" placeholder="请输入模型ID" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExecutionHistory,
  getExecutionHistory,
  delExecutionHistory,
  addExecutionHistory,
  updateExecutionHistory,
} from "@/api/executionHistory/executionHistory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeSelect } from "@/api/system/user";

export default {
  name: "ExecutionHistory",
  components: { Treeselect },
  dicts: ["execution_history_result"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史记录表格数据
      executionHistoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        introduction: null,
        executionTime: null,
        result: null,
        report: null,
        detail: null,
        modelId: null,
        department: null,
        departmentCode: null,
        doubtCount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 部门树选项
      deptOptions: undefined,
    };
  },
  created() {
    const type = this.$route.query.type;

    console.log("this.$route-----------", this.$route.query.men);
    console.log("this.$route-----------", this.$route.query.month);
    if (type) {
      if (type === "失败") {
        this.queryParams.result = 1;
      } else if (type === "成功") {
        this.queryParams.result = 0;
      }
    }
        const men = this.$route.query.men;
    const month = this.$route.query.month;
    if (men) {
      this.queryParams.department = men;
    }
    if (month) {
      this.queryParams.executionTime = month;
    }

    this.getList(type);
    this.getDeptTree();
  },
  methods: {
    //序号设置
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * 10 + index + 1;
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
        console.log("this.deptOptions", this.deptOptions);
      });
    },
    /** 查询历史记录列表 */
    getList(type) {
      this.loading = true;

      listExecutionHistory(this.queryParams).then((response) => {
        if (type) {
          if (type === "失败") {
            this.queryParams.result = "失败";
          } else if (type === "成功") {
            this.queryParams.result = "成功";
          }
        }
        this.executionHistoryList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        introduction: null,
        executionTime: null,
        result: null,
        report: null,
        detail: null,
        createTime: null,
        updateTime: null,
        modelId: null,
        department: null,
        departmentCode: null,
        doubtCount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加历史记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getExecutionHistory(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改历史记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateExecutionHistory(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExecutionHistory(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除历史记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delExecutionHistory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "executionHistory/executionHistory/export",
        {
          ...this.queryParams,
        },
        `executionHistory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
